package com.woniu.interception;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;

import com.woniu.pojo.Staff;
import com.woniu.service.StaffService;

public class SessionInterceptor implements HandlerInterceptor{
	@Autowired
	StaffService staffService;
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {
		// TODO Auto-generated method stub
		//System.out.println("ִ��������");
		Subject currentUser = SecurityUtils.getSubject();
		//System.out.println(currentUser.isAuthenticated());
		//System.out.println(currentUser.isRemembered());
        //�ж��û���ͨ����ס�ҹ����Զ���¼,��ʱsessionʧЧ
        if(!currentUser.isAuthenticated() && currentUser.isRemembered()){
            try {
            	   Session session = currentUser.getSession();
            	   if(session.getAttribute("loginStaff")==null){
		            	Staff staff=(Staff)currentUser.getPrincipal(); 
		            	String userName=staff.getStaff_job();
		            	System.out.println("userNAME"+userName);
		              
		                System.out.println(staff);
		            
		             
		                session.setAttribute("loginStaff",staff);
        
            	   }
            }catch (Exception e){
                //�Զ���¼ʧ��,��ת����¼ҳ��
            	
            	
                response.sendRedirect(request.getContextPath()+"/login.jsp");
                return false;
            }
           
        }
        return true;
	}
}
